//
//  AppDelegate.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "AppDelegate.h"
#import "FontSizeSingleTon.h"
#import "CaipuViewController.h"
#import "RemenViewController.h"
#import "ShicaiViewController.h"
#import "WodeViewController.h"
#import "WelcomePage.h"
#define ScreenHeight (NSInteger)[UIScreen mainScreen].bounds.size.height
@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];

    //状态栏白色
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    [[UINavigationBar appearance]setBarTintColor:[UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1]];
    //返回按钮
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
   //TabBar颜色
    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1]];
    CaipuViewController *vc1 = [[CaipuViewController alloc]init];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
    vc1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"菜谱" image:[UIImage imageNamed:@"caicai@2x"] tag:1001];
    
    RemenViewController *vc2 = [[RemenViewController alloc]init];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    vc2.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"热门" image:[UIImage imageNamed:@"remenhuo@2x"] tag:1002];
     
    ShicaiViewController *vc3 = [[ShicaiViewController alloc]init];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    vc3.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"食材" image:[UIImage imageNamed:@"shucai@2x"] tag:1003];
    
    WodeViewController *vc4 = [[WodeViewController alloc]init];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    vc4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-sichu@2x"] tag:1004];
    
    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    tabBarController.viewControllers = @[nav1,nav2,nav3,nav4];
    tabBarController.delegate = self;
    
    _window.rootViewController = tabBarController;
    
    //判断网络状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkState) name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    //让Reachability对象开启被监听状态
    [self.conn startNotifier];
    [self checkNetworkState];
    
    //获得字体大小数组
    [self createFontSize];
    
    //app是否是第一次运行
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"first9"]) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"first9"];
        WelcomePage *welcomeView = [[WelcomePage alloc]initWithFrame:_window.frame];
        [_window addSubview:welcomeView];
        
    }
    return YES;
}
-(void)createFontSize
{
    NSMutableArray *array = [NSMutableArray array];
    switch (ScreenHeight) {
        case 480:
            [array addObject:@"14"];
            [array addObject:@"12"];
            [array addObject:@"10"];
            break;
        case 568:
            [array addObject:@"16"];
            [array addObject:@"12"];
            [array addObject:@"10"];
            break;
        case 667:
            [array addObject:@"18"];
            [array addObject:@"14"];
            [array addObject:@"12"];
            break;
        case 736:
            [array addObject:@"20"];
            [array addObject:@"16"];
            [array addObject:@"14"];
            break;
        default:
            break;
    }
    [[FontSizeSingleTon shareFontSize] setArray:array];
}
- (void)checkNetworkState
{
    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    
    // 2.检测手机是否能上网络(WIFI\3G\2G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    // 3.判断网络状态
    // currentReachabilityStatus方法获取网络连接状态，如果网络连接状态返回NotReachable，则表明这种类型的网络暂未连接。
    // 有WiFi 和手机网络 用WiFi
    // 没有WiFi用手机网络
    // 没网络 就 没有 网络(ReachableViaWiFi,ReachableViaWWAN,NotReachable)
    if ([wifi currentReachabilityStatus] != NotReachable) {
        // 有wifi
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"当前网络状态为wifi" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alview show];
    
        
    } else if ([conn currentReachabilityStatus] != NotReachable) {
        // 没有使用wifi, 使用手机自带网络进行上网
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"请注意哦,当前网络状态为2G/3G/4G" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alview show];

    }
    else { // 没有网络
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"网络异常,请检查网络" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alview show];

    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
